﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ServerBackupAndMail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void chkEmail_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEmail.Checked == true)
            {
                grpEmail.Enabled = true;
            }
            else
            {
                grpEmail.Enabled = false;
            }
        }

        private void chkEnable_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEnable.Checked == true)
            {
                grpMain.Enabled = true;
            }
            else
            {
                grpMain.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fireBackup();
        }

        private void fireBackup()
        {
            if (DateTime.Now.Date >= dtWeeklyFrom.Value.Date && DateTime.Now.Date <= dtWeeklyTo.Value.Date)
            {
                startBackup("C:\\Backup\\");
            }
        }

        private void startBackup(string destination)
        {
            string attachment = "";
            string destDBBackup = "DhruvaCreation_" + DateTime.Now.ToString("dd_MMM_yyyy") + ".bak";
            Utils.SqlBackup(@".\SQLEXPRESS", "DhruvaCreation", destination + "\\" + destDBBackup);
            Utils.zipFile(destination + destDBBackup, destination + destDBBackup.Replace(".bak", ".zip"));
            attachment += destination + destDBBackup.Replace(".bak", ".zip");
            File.Delete(destination + destDBBackup);

            Utils.CopyLastetDirectory(@"C:\temp2\Kencha Ji\kascc websites\kascc websites\SGN Nursing", @"C:\Backup\App_" + DateTime.Now.ToString("dd_MMM_yyyy"), new DateTime(2012, 7, 19));
            Utils.zipFolder(@"C:\Backup\App_" + DateTime.Now.ToString("dd_MMM_yyyy"), @"C:\Backup\App_" + DateTime.Now.ToString("dd_MMM_yyyy") + ".zip");
            Directory.Delete(@"C:\Backup\App_" + DateTime.Now.ToString("dd_MMM_yyyy"),true);

            attachment += @",C:\Backup\App_" + DateTime.Now.ToString("dd_MMM_yyyy") + ".zip";

            Utils.sendEmail("deep.k.rai@gmail.com", "Deepak Rai", "nirmasuper4027", "deep.k.rai@gmail.com", "", "Test Subject", "Test Body", attachment);
        }
    }
}
